package com.aiju.cash.ui.setting;

import android.os.Bundle;
import android.view.View;

import com.aiju.app.config.ConfigManager;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.LoginApi;
import com.aiju.cash.ui.login.LoginActivity;
import com.aiju.cash.widgit.LineEditText;
import com.aiju.cash.widgit.TwoLineEditText;
import com.aiju.common.ActivityStack;
import com.aiju.utils.CheckUtil;
import com.aiju.utils.KeyBoardUtils;
import com.aiju.utils.L;
import com.aiju.utils.PreferencesUtils;
import com.aiju.utils.T;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-12-19 13:49
 * @version: V1.0
 * @Description: 设置新密码
 */
public class UserPasswordActivity extends BaseTitleActivity {

    private LineEditText oldEditText, newEditText;
    private TwoLineEditText repeatEditText;

    private LoginApi mLoginApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        setCenterTitle("设置新密码");
        setRightTitle("保存");

        oldEditText = (LineEditText) findViewById(R.id.ev_pw_old);
        newEditText = (LineEditText) findViewById(R.id.ev_pw_new);
        repeatEditText = (TwoLineEditText) findViewById(R.id.ev_pw_new_repeat);

        oldEditText.setLineColor(getResources().getColor(R.color.app_divider_color));
        newEditText.setLineColor(getResources().getColor(R.color.app_divider_color));
        repeatEditText.setLineColor(getResources().getColor(R.color.app_divider_color));
        KeyBoardUtils.openKeybord(oldEditText, this);

        mLoginApi = new LoginApi();
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPassword = oldEditText.getText().toString();
                String newPassword = newEditText.getText().toString();
                String repeatPassword = repeatEditText.getText().toString();
                if (CheckUtil.checkPassword(oldPassword) && CheckUtil.checkPassword(newPassword) &&
                        CheckUtil.checkPassword(repeatPassword)) {
                    if (!newPassword.equals(repeatPassword)) {
                        T.ToastShort("您输入的新密码不一致，请重新输入");
                        return;
                    }
                    if (newPassword.equals(oldPassword)) {
                        T.ToastShort("新密码和原始密码密码相同，请重新输入");
                        return;
                    }
                    updatePassword(oldPassword, newPassword);
                } else {
                    T.ToastShort("密码仅支持8-20位字母、数字及下划线，请重新输入");
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        KeyBoardUtils.closeKeybord(oldEditText, this);
    }

    /**
     * 更改密码，成功后，注销登录
     */
    private void updatePassword(String oldPassword, String newPassword) {
        mLoginApi.updatePassword(mUser.getUserName(), oldPassword, newPassword, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                DataJson json = new DataJson(response);
                if (json.isSuccess()) {
                    loginOut();
                } else {
                    T.ToastShort("密码修改失败");
                    L.e(json.getMsg());
                }
            }
        });
    }

    /**
     * 注销登录
     */
    private void loginOut() {
        mLoginApi.logout(mUser.getToken(), ConfigManager.getInstance().getRegId(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                DataJson json = new DataJson(response);
                if (json.isSuccess()) {
                    T.ToastShort("密码修改成功");
                    ActivityStack.finishAll();
                    startActivity(LoginActivity.class);
                    PreferencesUtils.putString(UserPasswordActivity.this, "password", "");
                } else {
                    T.ToastShort("注销失败");
                    L.e(json.getMsg());
                }
            }
        });
    }
}
